import { Avatar, Button, Checkbox, Form, Input, message } from "antd";
import { useNavigate } from "react-router-dom";
import "./index.less";
import { IResetPassword, IRetrievePassword } from "@/types/login";
import { resetPassword, retrievePassword, sendSMS } from "@/api/employee";
import { useEffect, useState } from "react";
import { captcha } from "@/api/common";
// 找回密码
const RetrievePasswordPage = () => {
  const navigate = useNavigate();
  const [captchaSrc, setCaptchaSrc] = useState("");
  const [phone, setPhone] = useState("");
  const [codeImg, setCodeImg] = useState("");
  useEffect(() => {
    queryCaptcha();
  }, []);
  const queryCaptcha = async () => {
    const res = await captcha();
    if (res) {
      setCaptchaSrc(res);
    }
    1;
  };
  const handleClickSMS = async () => {
    if (phone.length !== 11) {
      message.warning("请输入11位的手机号");
      return;
    }
    if (!codeImg) {
      message.warning("请输入图形验证码");
      return;
    }
    const res = await sendSMS({ phone, code: codeImg });
    if (res) {
      message.success("验证码，发送成功，默认1234");
    }
  };
  const onFinish = async (data: IRetrievePassword) => {
    if (data.newPassword.length < 6) {
      message.warning("新长度不能低于5位");
      return;
    }
    const res = await retrievePassword(data);
    if (res) {
      navigate("/login");
    }
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };

  //   返回
  const handleReturn = () => {
    navigate(-1);
  };

  return (
    <Form
      name="basic"
      labelCol={{ span: 8 }}
      wrapperCol={{ span: 16 }}
      style={{ maxWidth: 600 }}
      initialValues={{ remember: true }}
      onFinish={onFinish}
      onFinishFailed={onFinishFailed}
      autoComplete="off"
      className="reset-wrap"
    >
      <Form.Item
        label="手机号"
        name="phone"
        rules={[{ required: true, message: "请输入手机号" }]}
      >
        <Input
          placeholder="请输入手机号"
          onChange={(e) => setPhone(e.target.value)}
        />
      </Form.Item>
      <Form.Item
        label="图形验证码"
        name="code"
        rules={[{ required: true, message: "图形验证码" }]}
      >
        <div className="flex">
          <Input
            placeholder="图形验证码"
            onChange={(e) => setCodeImg(e.target.value)}
          />
          <img
            className="captcha-img"
            src={captchaSrc}
            onClick={queryCaptcha}
          />
        </div>
      </Form.Item>
      <Form.Item
        label="短信验证码"
        name="SMSCode"
        rules={[{ required: true, message: "短信验证码" }]}
      >
        <div className="flex">
          <Input placeholder="短信验证码" />
          <Button type="primary" onClick={handleClickSMS}>
            发送验证码
          </Button>
        </div>
      </Form.Item>
      <Form.Item
        label="新密码"
        name="newPassword"
        rules={[{ required: true, message: "请输入新密码" }]}
      >
        <Input.Password placeholder="请输入新密码" />
      </Form.Item>

      <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
        <Button className="margin-bottom-16px" block onClick={handleReturn}>
          返回
        </Button>
        <Button block type="primary" htmlType="submit">
          修改
        </Button>
      </Form.Item>
    </Form>
  );
};

export default RetrievePasswordPage;
